package com.lagou.rpc.consumer;

import com.lagou.rpc.consumer.client.RpcClient;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * WebServerFactoryCustomizer也就是一种定制器类：需要通过新建自定义的WebServerFactoryCustomizer类来实现属性配置修改。
 */
@SpringBootApplication
public class ClientBootstrapApplication implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

    private static Map<String, RpcClient> rpcClientMap = new ConcurrentHashMap<String, RpcClient>();
    private static final String zookeeperAddress = "127.0.0.1:2181";
    private static final String ROOT_NODE = "/lg-provider";


    public static void main(String[] args) {
        SpringApplication.run(ClientBootstrapApplication.class, args);
        //启动定时刷新节点响应时间
        ZkClient zkClient = new ZkClient(zookeeperAddress);
        refeshRespsTime(zkClient);
    }

    /**
     * 设置服务端口
     */
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        factory.setPort(8098);
    }


    /**
     * 定时刷新节点的响应时间
     * @param zkClient
     */
    public static void refeshRespsTime(final ZkClient zkClient){
        new Thread(new Runnable() {
            public void run() {
                while (true){
                    try {
                        List<String> serviceList = zkClient.getChildren(ROOT_NODE);
                        if (serviceList != null && serviceList.size() > 0){
                            for (int i = 0; i < serviceList.size(); i++) {
                                String ipHostNode = ROOT_NODE + "/" + serviceList.get(i);
                                if(zkClient.exists(ipHostNode)){
                                    Object respCtime = zkClient.readData(ipHostNode);
                                    if(respCtime != null &&  !"".equals(respCtime)){
                                        try {
                                            Long createTimes = respCtime != null ? Long.parseLong(respCtime.toString()) : 0;
                                            Long currentTimes = System.currentTimeMillis();
                                            Long difTimes = currentTimes-createTimes;
                                            System.out.println("currentTimes----------:" + currentTimes);
                                            System.out.println("createTimes----------:" + createTimes);
                                            System.out.println("difTimes----------:" + difTimes);
                                            if(createTimes != 0 && difTimes != null && difTimes > 5000){//超过五秒未更新需要清空节点响应时间记录
                                                System.out.println("服务端[" + ipHostNode + "]5秒内无新请求，清空响应时间为0----------");
                                                zkClient.writeData(ipHostNode,0);
                                            }
                                        }catch (Exception e){
                                            e.printStackTrace();
                                        }

                                    }
                                }
                            }
                        }
                        System.out.println("定时刷新节点执行时间");
                        Thread.sleep(5000);//五秒一次
                    } catch (InterruptedException exception) {
                        exception.printStackTrace();
                    }
                }
            }
        }).start();
    }

}
